﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AP.LUNA.DataEntities;
using AP.LUNA.DataAccess;
using System.Web;
using System.Data;
using System.Data.Objects;


namespace AP.LUNA.BusinessLogic
{
    public abstract class BLAstract
    {
        public LunaEntities GetContext()
        {
            string contextKey = "contextKey_" + HttpContext.Current.GetHashCode().ToString("x");
            if(!HttpContext.Current.Items.Contains(contextKey))
                HttpContext.Current.Items.Add(contextKey, new LunaEntities());
            return HttpContext.Current.Items[contextKey] as LunaEntities;
        }

        public void HandleOptimisticConcurrency(OptimisticConcurrencyException ex)
        {
            if (ex.StateEntries != null)
            {
                foreach (var item in ex.StateEntries)
                {
                    GetContext().Refresh(RefreshMode.StoreWins, item.Entity);
                }
            }
        }
    }
}
